
package cn.enjoy.tool.exception;

import cn.enjoy.tool.api.R;
import cn.enjoy.tool.api.ResultCode;
import cn.enjoy.tool.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常拦截，业务、安全、Feign远程调用 异常
 * 未知异常拦截，Throwable
 * Order 排序优先级低
 *
 * @author K
 */
@Slf4j
@RestControllerAdvice(basePackages = "cn.enjoy")
public class EnjoyRestExceptionTranslator {

    @ExceptionHandler(ServiceException.class)
    public R handleError(ServiceException e) {
        log.error("业务异常", e);
        return R.fail(e.getResultCode(), e.getMessage());
    }

    /**
     * 捕获所有的异常
     */
    @ExceptionHandler({Throwable.class})
    public R handleError(Throwable e) {
        log.error("服务器异常", e);
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, Func.isEmpty(e.getMessage()) ? ResultCode.INTERNAL_SERVER_ERROR.getMessage() : e.getMessage());
    }

}
